/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.controller;

import org.jefb.service.IUserSessionService;
import org.jefb.web.model.DirectoryMananagerModel;
import org.jefb.web.model.MenuModel;
import org.jefb.web.model.MenuModel.MenuBlock;
import org.jefb.web.model.item.MenuItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Include;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.North;

@org.springframework.stereotype.Component
@Scope("prototype")
public class MainController extends GenericForwardComposer{
	
	private static final long serialVersionUID = 1L;
	
	protected Listbox workspaceMenuList;
	protected Listbox adminMenuList;
	protected Include include;
	
	//models
	@Autowired
	private DirectoryMananagerModel directoryManagerModel;
	@Autowired
	private IUserSessionService userSessionService;
	
	
	protected North mainNorth;
	public void onClick$testButton(Event event){
		if(mainNorth.isOpen()){
			mainNorth.setOpen(false);
		}else{
			mainNorth.setOpen(true);
		}
	}
	
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		
		MenuModel workspaceMenuModel = new MenuModel(MenuBlock.COMMON, userSessionService);
		workspaceMenuList.setModel(workspaceMenuModel);
		workspaceMenuList.setItemRenderer(workspaceMenuModel);
		
		MenuModel adminMenuModel = new MenuModel(MenuBlock.ADMINISTRATION, userSessionService);
		adminMenuList.setModel(adminMenuModel);
		adminMenuList.setItemRenderer(adminMenuModel);
		
		if(!workspaceMenuModel.isEmpty()){
			MenuItem menuItem = (MenuItem)workspaceMenuModel.get(0);
			include.setSrc(menuItem.getPageName());
		}
	}
	
	public void onSelect$adminMenuList(Event event){
		MenuItem menuItem = 
			(MenuItem)adminMenuList.getSelectedItem().getAttribute("menuItem");
		include.setSrc(menuItem.getPageName());
		adminMenuList.clearSelection();
	}
	
	public void onSelect$workspaceMenuList(Event event){
		MenuItem menuItem = 
			(MenuItem)workspaceMenuList.getSelectedItem().getAttribute("menuItem");
		directoryManagerModel.resetModel();
		include.setSrc(menuItem.getPageName());
		workspaceMenuList.clearSelection();
	}

	public void setDirectoryManagerModel(DirectoryMananagerModel directoryManagerModel) {
		this.directoryManagerModel = directoryManagerModel;
	}

	public DirectoryMananagerModel getDirectoryManagerModel() {
		return directoryManagerModel;
	}

	public void setUserSession(IUserSessionService userSessionService) {
		this.userSessionService = userSessionService;
	}

	public IUserSessionService getUserSession() {
		return userSessionService;
	}

}
